//
//  BuyCarMarketVC.m
//  New_Runway
//
//  Created by mack on 2018/3/8.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "BuyCarMarketVC.h"
#import "BuyCarMarketModel.h"

@interface BuyCarMarketVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableview;
@property (nonatomic, strong) NSMutableArray *marketArray;

@end

@implementation BuyCarMarketVC

-(NSMutableArray *)marketArray{
    
    if (!_marketArray) {
        _marketArray = [[NSMutableArray alloc] init];
    }
    return _marketArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationBarView.titleLabel.text = @"市场";
    [self initTableview];
    [self loadMarketList];
    
}
-(void)initTableview{
    //    kSelfWeak;
    self.tableview = [[UITableView alloc] init];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    _tableview.estimatedRowHeight = 0;
    _tableview.estimatedSectionHeaderHeight = 0;
    _tableview.estimatedSectionFooterHeight = 0;
    if (@available(iOS 11.0, *)) {
        self.tableview.contentInsetAdjustmentBehavior = UIApplicationBackgroundFetchIntervalNever;
    } else {
        //        self.automaticallyAdjustsScrollViewInsets = false;
    }
    self.tableview.backgroundColor = MAIN_COLOR();
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CELL"];
    [self.view addSubview:self.tableview];
    
    self.tableview.frame = CGRectMake(0, self.navH, Screen_width,Screen_height - self.BotH - self.navH);
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.marketArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BuyCarMarketModel *model = self.marketArray[indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL"];
    UILabel *line = [[UILabel alloc] init];
    line.backgroundColor = UIColorHex(0xf5f5f5);
    line.frame = CGRectMake(0, 0, Screen_width, 0.5);
    [cell.contentView addSubview:line];
    cell.textLabel.text = model.marketName;
    cell.tintColor = MAIN_NAV_COLOR();
    cell.textLabel.font = [UIFont systemFontOfSize:12];
    if (_selIndex == indexPath) {
        cell.textLabel.textColor = MAIN_NAV_COLOR();
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }else {
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.textLabel.textColor = MAIN_FONT_COLOR();
        
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    UITableViewCell *celled = [tableView cellForRowAtIndexPath:_selIndex];
    celled.accessoryType = UITableViewCellAccessoryNone;
    celled.textLabel.textColor = MAIN_FONT_COLOR();
    //记录当前选中的位置索引
    _selIndex = indexPath;
    BuyCarMarketModel *model = self.marketArray[indexPath.row];
    self.MarketBlock(model.marketId,indexPath);
    //当前选择的打勾
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    cell.textLabel.textColor = MAIN_NAV_COLOR();
    
    [self.navigationController popViewControllerAnimated:YES];
}
//获取市场
-(void)loadMarketList{
    NSString *url = [NSString stringWithFormat:@"%@%@",Api,allMarketListApi];
    
    NSDictionary *params = @{
                             @"marketId":@"1",                             
                             };
    [YQNetworking postWithUrl:url refreshRequest:YES cache:NO params:params progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
        
    } successBlock:^(id response) {
        
        if ([response[@"code"] isEqual:@1]) {
            [self.marketArray removeAllObjects];
            NSArray *array = response[@"result"];
            for (NSDictionary *dic in array) {
                BuyCarMarketModel *model = [[BuyCarMarketModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [self.marketArray addObject:model];
            }
            
        }
        [self.tableview reloadData];
    } failBlock:^(NSError *error) {
        [ShowMessage showHintMessage:@"请求失败!" duration:2.0 withRemindId:remindFail];

    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
